package top.gangster.lunan.contractservice.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.gangster.lunan.contractservice.entity.ContractType;
import top.gangster.lunan.contractservice.service.ContractTypeService;
import top.gangster.lunan.result.Result;

@RestController
@RequestMapping("/contractservice/type")
public class ContractTypeController {
	@Autowired
	ContractTypeService contractTypeService;

	/**
	 * 获取所有合同类型
	 * @return typeList
	 */
	@GetMapping
	public Result getAllContractType() {
		List<ContractType> typeList = contractTypeService.list(null);
		return Result.ok().data("types",typeList);
	}

	/**
	 * 获取指定id的合同类型
	 * @param typeId 合同类型id
	 * @return 合同类型对象
	 */
	@GetMapping("/{typeId}")
	public Result getContractTypeById(@PathVariable Integer typeId) {
		ContractType type = contractTypeService.getById(typeId);
		return Result.ok().data("type",type);
	}

	/**
	 * 添加合同类型
	 * @param type 合同类型
	 * @return Result
	 */
	@PostMapping
	public Result saveContractType(@RequestBody ContractType type) {
		boolean b = contractTypeService.save(type);
		if (b) {
			return Result.ok().message("添加合同类型成功");
		}
		return Result.error().message("添加合同类型失败");
	}

	/**
	 * 更新合同类型信息
	 * @param type 合同类型
	 * @return Result
	 */
	@PutMapping
	public Result updateContractType(@RequestBody ContractType type) {
		boolean b = contractTypeService.updateById(type);
		if (b) {
			return Result.ok().message("更新合同类型信息成功");
		}
		return Result.error().message("更新合同类型信息失败");
	}

	/**
	 * <h2>合同类型在一般情况下不会被删除！！！</h2>
	 * 删除合同类型
	 * @param typeId 合同类型id
	 * @return Result
	 */
	@DeleteMapping("/{typeId}")
	public Result deleteContractType(@PathVariable Integer typeId) {
		boolean b = contractTypeService.removeById(typeId);
		if (b) {
			return Result.ok().message("删除合同类型成功");
		}
		return Result.error().message("删除合同类型失败");
	}
}
